package com.erhuogo.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.bean.Response;
import com.erhuogo.entity.Province;
import com.erhuogo.enumerate.Result;
import com.erhuogo.service.ProvinceService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 省份Action
 * @author aibinxiao
 * @date 2016年10月26日 上午11:34:13
 */
public class ProvinceAction extends ActionSupport {
	private static final long serialVersionUID = 2963112787745862811L;
	private static Logger logger = LoggerFactory.getLogger(ProvinceAction.class);
	
	private Province province;
	
	public Province getProvince() {
		return province;
	}

	public void setProvince(Province province) {
		this.province = province;
	}

	//注入ProvinceService
	private ProvinceService provinceService;
	
	public ProvinceService getProvinceService() {
		return provinceService;
	}

	public void setProvinceService(ProvinceService provinceService) {
		this.provinceService = provinceService;
	}
	
	// 返回页面数据
	private Response response = new Response();

	public Response getResponse() {
		return response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}

	/**
	 * 1.获取所有的省份信息
	 * @return
	 */
	public String getAllProvinces(){
		List<Province> listProvices = provinceService.findAllProvince();
		if(listProvices != null && listProvices.size()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("获取省份列表成功");
			response.setData(listProvices);
		} else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("获取省份列表失败/列表为空");
			response.setData(listProvices);
		}
		return "allProvinces";
	}
	
	/**
	 * 2.分页条件查询省份
	 * @return
	 */
	public String listPage(){
		String page = ServletActionContext.getRequest().getParameter("page");
		String rows = ServletActionContext.getRequest().getParameter("rows");
		String id = ServletActionContext.getRequest().getParameter("id");
		String name = ServletActionContext.getRequest().getParameter("name");
		String startDate = ServletActionContext.getRequest().getParameter("startDate");
		String endDate = ServletActionContext.getRequest().getParameter("endDate");
		GridPageBean<Province> listPage = provinceService.findByPage(page, rows, id, name, startDate, endDate);
		if(listPage!=null && listPage.getRecords()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("查询成功");
		}else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("查询失败");
		}
		response.setData(listPage);
		return "listPageSUCCESS";
	}
	
	/**
	 * 3.根据id查询省份
	 * @return
	 */
	public String findById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		Province province = provinceService.findById(id);
		if(province!=null){
			response.setData(province);
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("查询成功");
		}else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("查询失败");
		}
		return "findByIdSUCCESS";
	}
	
	/**
	 * 4.保存或者更新省份信息
	 * @return
	 */
	public String saveProvince(){
		String id = ServletActionContext.getRequest().getParameter("id");
		String name = ServletActionContext.getRequest().getParameter("name");
		try {
			provinceService.save(id,name);
			response.setResult(Result.SUCCESS.toString());
		} catch (Exception e) {
			response.setResult(Result.FAILED.toString());
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return "saveSUCCESS";
	}
	
	/**
	 * 5.开启或者屏蔽省份信息
	 * @return
	 */
	public String updateStatusById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		String status = ServletActionContext.getRequest().getParameter("status");
		try {
			provinceService.updateStatus(id,status);
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("更新状态成功");
		} catch (Exception e) {
			response.setResult(Result.FAILED.toString());
			response.setMessage("更新状态失败");
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return "updateStatusSUCCESS";
	}
}
